package com.qf.blogsystem.service.impl;

import com.qf.blogsystem.core.dao.BaseDao;
import com.qf.blogsystem.core.service.impl.BaseServiceImpl;
import com.qf.blogsystem.core.utils.EncryptUtils;
import com.qf.blogsystem.core.utils.UUIDUtils;
import com.qf.blogsystem.dao.UserDao;
import com.qf.blogsystem.dao.impl.UserDaoImpl;
import com.qf.blogsystem.entity.User;
import com.qf.blogsystem.service.UserService;

import java.util.Date;
import java.util.List;

/**
 * @author Crazy
 * @version 1.0
 * @description: 用户业务层实现类
 * @date 2022/12/6 0:46
 */
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {
    /**
     * 创建用户Dao实现类对象
     */
    private UserDao userDao=new UserDaoImpl();

    @Override
    protected BaseDao<User> getBaseDao() {
        return userDao;
    }

    /**
     * 用户注册
     * @param user
     * @return boolean
     * @throws Exception
     */
    @Override
    public boolean save(User user) throws Exception {
        //密码加密
        user.setPassword(EncryptUtils.encrypt(user.getPassword()));
        //设置用户的状态
        user.setStatus(0);
        //创建时间
        user.setCreateTime(new Date());
        //获得激活码
        user.setActivationCode(UUIDUtils.getActivationKey());
        //用户类型
        user.setType(0);

        //入库
        return userDao.insert(user)>0;
    }

    /**
     * 根据激活码查询用户信息
     * @param activationCode
     * @return User
     * @throws Exception
     */
    @Override
    public User byActivationCode(String activationCode) throws Exception {
        return userDao.selectByActivationCode(activationCode);
    }

    /**
     * 用户登录验证
     * @param username
     * @param password
     * @return User
     * @throws Exception
     */
    @Override
    public User login(String username, String password) throws Exception {
        //根据用户名查询用户信息
        User user = userDao.selectByUsername(username);

        if (user == null) {
            //用户名不存在
            return null;
        }

        //校验密码
        if (!EncryptUtils.check(password, user.getPassword())) {
            //密码错误
            return null;
        }

        return user;
    }

    /**
     * 更改头像
     * @param id
     * @param filePath
     * @throws Exception
     */
    @Override
    public boolean updateHeaderUrl(Integer id, String filePath) throws Exception {
        return userDao.updateHeaderUrl(id,filePath)>0;
    }

    /**
     * 根据用户名查询用户信息
     * @param user
     * @return User
     * @throws Exception
     */
    @Override
    public User byUsername(User user) throws Exception {
        return userDao.selectByUsername(user.getUsername());
    }

    /**
     * 根据邮箱查询用户信息
     * @param user
     * @return User
     * @throws Exception
     */
    @Override
    public User byEmail(User user) throws Exception {
        return userDao.selectByEmail(user.getEmail());
    }

}
